//
//  GJMapSearchBarView.m
//  xmgjAppFront
//
//  Created by Zeki on 2020/7/28.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "GJMapSearchBarView.h"
#import "ZHFAddTitleAddressView.h"
@interface GJMapSearchBarView ()<UITextFieldDelegate>
@property (nonatomic, strong)UILabel *cityLabel;//城市
@property (nonatomic, strong)UILabel *countryLabel;//县区
@property (nonatomic, strong)UIButton *selectedButton;//选择市县
@property (nonatomic, strong)UITextField *addressTextField;//详细地址
@property (nonatomic, strong)UIImageView *iconImageView;//点击图标
@property (nonatomic, strong)UIView *lineView;//竖线
@property (nonatomic, strong)GJLocationUtil *util;
@property (nonatomic, strong)ZHFAddTitleAddressView *addTitleAddressView;
@end

@implementation GJMapSearchBarView

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [self initlizationSubViews];
    self.backgroundColor = HexColor(#F5F5F5);
    self.layer.cornerRadius = 4;
    self.layer.masksToBounds = YES;
  }
  return self;
}


- (void)initlizationSubViews{
  [self addSubview:self.cityLabel];
  [self addSubview:self.countryLabel];
  [self addSubview:self.selectedButton];
  [self addSubview:self.iconImageView];
  [self addSubview:self.lineView];
  [self addSubview:self.addressTextField];
  
  [self.cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(12);
    make.height.mas_equalTo(20);
    make.centerY.equalTo(self);
    make.width.mas_equalTo(42);
  }];
  [self.countryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.height.equalTo(self.cityLabel);
    make.left.equalTo(self.cityLabel.mas_right).offset(6);
    make.width.mas_equalTo(42);
  }];
  [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.cityLabel);
    make.height.width.mas_equalTo(10);
    make.left.equalTo(self.countryLabel.mas_right).offset(5);
  }];
  
  [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.iconImageView.mas_right).offset(5);
    make.width.mas_equalTo(0.5);
    make.top.mas_equalTo(7);
    make.bottom.mas_equalTo(-7);
  }];
  
  [self.addressTextField mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.lineView.mas_right).offset(8);
    make.height.centerY.equalTo(self.cityLabel);
    make.right.equalTo(self.mas_right).offset(-12);
  }];
//
  //button
  [self.selectedButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.top.bottom.equalTo(self.cityLabel);
    make.right.equalTo(self.countryLabel.mas_right);
  }];

  
}
/// TODO: actions
- (void)buttonClickAction:(UIButton *)sender{
  switch (sender.tag) {
    case 99:
    {
      //选择地址
      [self.addressTextField endEditing:YES];
      [self addressChooseAction];
    }
      break;
    default:
      break;
  }
}
#pragma mark -- addressChoose
- (void)addressChooseAction{
  self.addTitleAddressView = [[ZHFAddTitleAddressView alloc]init];
  self.addTitleAddressView.title = @"选择地址";
  self.addTitleAddressView.defaultHeight = 350;
  self.addTitleAddressView.titleScrollViewH = 37;
  self.addTitleAddressView.numberOfLevel = 3;
  if (self.addTitleAddressView.titleIDMarr.count > 0) {
    self.addTitleAddressView.isChangeAddress = true;
  }
  else{
    self.addTitleAddressView.isChangeAddress = false;
  }
  @weakify(self);
  self.addTitleAddressView.callBackBlcok = ^(NSDictionary *addressDic) {
    @strongify(self);
    self.dataDictionary = @{@"city":[addressDic[@"city"] objectForKey:@"name"],
                            @"area":[addressDic[@"city"] objectForKey:@"name"]
    };

    if (self.addressChooseBackBlock) {
      self.addressChooseBackBlock(addressDic);
    }
    [self.addTitleAddressView removeFromSuperview];
  };
  
  [[UIApplication sharedApplication].keyWindow addSubview:[self.addTitleAddressView initAddressView]];
  [self.addTitleAddressView addAnimate];
}


#pragma mark -- UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
  if (self.textFieldBlock) {
    self.textFieldBlock(@"", YES, NO);
  }
  return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
  
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
  /**< 当输入中文时,只获取中文部分,过滤掉多余的字母 */
  UITextRange *selectedRange = [textField markedTextRange];
  NSString * newText = [textField textInRange:selectedRange];
  if (newText.length > 0) {/**< 获取高亮部分 */
    return YES;
  }
  NSMutableString *string = [NSMutableString stringWithString:textField.text];
  if ([string containsString:@" "]) {/**< 过滤空格 */
    for (int i = 0; i < string.length; i++) {
      unichar c = [string characterAtIndex:i];
      NSRange range = NSMakeRange(i, 1);
      if (c == ' ') {
        [string deleteCharactersInRange:range];
      }
    }
  }
  //检索
  [[GJLocationUtil shareInstance] searchPOIWithKeyWords:string city:self.cityLabel.text complete:self.keyWordsPoiSearchBlock];
  [textField endEditing:YES];
  return YES;
}

- (void)textFieldDidChange:(UITextField*)textField{
  /**< 当输入中文时,只获取中文部分,过滤掉多余的字母 */
  UITextRange *selectedRange = [textField markedTextRange];
  NSString * newText = [textField textInRange:selectedRange];
  if (newText.length > 0) {/**< 获取高亮部分 */
    return;
  }
  NSMutableString *string = [NSMutableString stringWithString:textField.text];
  if ([string containsString:@" "]) {/**< 过滤空格 */
    for (int i = 0; i < string.length; i++) {
      unichar c = [string characterAtIndex:i];
      NSRange range = NSMakeRange(i, 1);
      if (c == ' ') {
        [string deleteCharactersInRange:range];
      }
    }
  }
  //检索
  [[GJLocationUtil shareInstance] searchPOIWithKeyWords:string city:self.cityLabel.text complete:self.keyWordsPoiSearchBlock];
  
}



#pragma mark -- getter
- (UILabel *)cityLabel{
  if (!_cityLabel) {
    _cityLabel = [UILabel new];
    _cityLabel.textColor = HexColor(#343332);
    _cityLabel.font = kMediumSystemFontOfSize(14);
  }
  return _cityLabel;
}
- (UILabel *)countryLabel{
  if (!_countryLabel) {
    _countryLabel = [UILabel new];
    _countryLabel.textColor = HexColor(#343332);
    _countryLabel.font = kMediumSystemFontOfSize(14);
  }
  return _countryLabel;
}
- (UIImageView *)iconImageView{
  if (!_iconImageView) {
    _iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mapModule_down"]];
  }
  return _iconImageView;
}
- (UIView *)lineView{
  if (!_lineView) {
    _lineView = [UIView new];
    _lineView.backgroundColor = HexColor(#CDCCC9);
  }
  return _lineView;
}
- (UITextField *)addressTextField{
  if (!_addressTextField) {
    _addressTextField = [[UITextField alloc]init];
    _addressTextField.placeholder = @"请输入关键字";
    _addressTextField.textColor = HexColor(#343332);
    _addressTextField.font = kSystemFontOfSize(14);
    _addressTextField.delegate = self;
    _addressTextField.returnKeyType = UIReturnKeySearch;
    _addressTextField.clearsOnBeginEditing = YES;
    [_addressTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  }
  return _addressTextField;
}


- (UIButton *)selectedButton{
  if (!_selectedButton) {
    _selectedButton = [UIButton new];
    _selectedButton.tag = 99;
    [_selectedButton addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  }
  return _selectedButton;
}

- (GJLocationUtil *)util{
  if (!_util) {
    _util = [GJLocationUtil new];
  }
  return _util;
}
#pragma mark -- setter
- (void)setDataDictionary:(NSDictionary *)dataDictionary{
  _dataDictionary = dataDictionary;
  self.cityLabel.text = [ dataDictionary[@"city"] gj_hasValue] ?  dataDictionary[@"city"] : @"";
  self.countryLabel.text = [dataDictionary[@"area"] gj_hasValue] ? dataDictionary[@"area"] : @"" ;
  CGFloat cityWD  =  [self.cityLabel.text gj_measureFrameWithFont:self.cityLabel.font size:CGSizeMake(CGFLOAT_MAX, self.cityLabel.height)].size.width;
  CGFloat countryWD  =  [self.countryLabel.text gj_measureFrameWithFont:self.countryLabel.font size:CGSizeMake(CGFLOAT_MAX, self.countryLabel.height)].size.width;
  [self.cityLabel mas_updateConstraints:^(MASConstraintMaker *make) {
    make.width.mas_equalTo(cityWD + 2);
  }];
  [self.countryLabel mas_updateConstraints:^(MASConstraintMaker *make) {
    make.width.mas_equalTo(countryWD + 2);
  }];
}
- (void)setAddress:(NSString *)address{
  _address = address;
  self.addressTextField.text = address;
  if (![address gj_hasValue]) {
    self.addressTextField.text = nil;
  }
}
@end
